package sifcom.Entidades.Factory;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import sifcom.Entidades.Zona;
import sifcom.Utilidades.Utilidades;
import sifcom.json.ManejoJson;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;

public class ZonaFactory {
	
	public ZonaFactory()
	{
		
	}
	
	public ZonaFactory(BufferedReader reader) throws JSONException, EntityNotFoundException
	{
		ObjectifyGenericDao<Zona> daoObjectify = new ObjectifyGenericDao<Zona>(Zona.class);
		
		Utilidades util = new Utilidades();
		
		JSONObject jsonSocio = util.getJsonObject(reader);
		
			//Delete
			if(jsonSocio.length() == 1 && jsonSocio.get("id") != null)
			{
				Key<Zona> keyCanton = new Key<Zona>(Zona.class,jsonSocio.getLong("id")); 
				
				Objectify ofy = daoObjectify.fact().begin();			
				
				ofy.delete(keyCanton);
			}
			
			//Create
			if(jsonSocio.length() > 1 && jsonSocio.get("id").toString().length() == 4)
			{
				Zona zona = new Zona();
				
				ManejoJson<Zona> mson = new ManejoJson<Zona>(Zona.class);
				mson.setData(zona, jsonSocio);
				
				Key<Zona> key =daoObjectify.put(zona);
				
				System.out.println(key.getId());
				System.out.println(key.getRaw());
			}
		
			//Update
			if(jsonSocio.length() > 1 && !jsonSocio.get("id").toString().equalsIgnoreCase("null"))
			{
				Key<Zona> key = new Key<Zona>(Zona.class,jsonSocio.getLong("id"));
				
				System.out.println("Key: "+key.getId());
				
				Zona Zona = daoObjectify.get(key);			
				
				ManejoJson<Zona> mson = new ManejoJson<Zona>(Zona.class);
				mson.setData(Zona, jsonSocio);
				daoObjectify.put(Zona);				
			}
	}
	
	public String getData() throws EntityNotFoundException{
		
		ObjectifyGenericDao<Zona> daoObjectify = new ObjectifyGenericDao<Zona>(Zona.class);		
		
		List<Zona> lst = daoObjectify.listByExample(new Zona());

		return setData(lst);
	}

	public String Search(String zona) throws EntityNotFoundException{
		
		ObjectifyGenericDao<Zona> daoObjectify = new ObjectifyGenericDao<Zona>(Zona.class);
		Zona zo = new Zona();
		if(zona.length()>=1)
			zo.setZona(zona);
		
		List<Zona> lst = daoObjectify.listByExample(zo);

		return setData(lst);
	}	
	
	private String setData(List<Zona> lst) throws EntityNotFoundException
	{
		JSONObject json = new JSONObject();
		List<String> array= new ArrayList<String>();
		
		for(Zona entidad : lst)
		{			
				try {
					json.put("id", entidad.getId());
					json.put("zona", entidad.getZona());
					
					array.add(json.toString());
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}								
		}						
		
		String myOut = "{\"success\":true,\"message\":\"Load Data 2\",\"data\":"+array.toString()+"}"; 
		
		return myOut;
	}
		
}
